export class ObservedTarget<T> {
    private target: T;
    private listener: ((val: T) => void)[] = [];

    constructor(target: T) {
        this.target = target;
    }

    get value() {
        return this.target;
    }

    set value(value: T) {
        if (value == this.target) return;
        this.target = value;
        this.listener.forEach((listener: ((val: T) => void)) => listener(value));
    }

    registerListener(listener: ((val: T) => void)): void {
        this.listener.push(listener);
    }

    unregisterListener(listener: ((val: T) => void)): void {
        this.listener = this.listener.filter((val) => val !== listener);
    }
}